package com.tsinghuabigdata.edu.ddmath.util;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.tsinghuabigdata.edu.ddmath.BuildConfig;
import com.tsinghuabigdata.edu.ddmath.constant.AppConst;
import com.tsinghuabigdata.edu.ddmath.receive.NetworkStateRecevie;
import com.tsinghuabigdata.edu.session.AppSession;

import java.util.List;

import roboguice.util.Strings;

/**
 * <p>
 * Created by yanshen@tsinghuabigdata.com on 2015/11/12.
 * </p>
 *
 * @author yanshen@tsinghuabigdata.com
 * @version V1.0
 * @packageName: com.tsinghuabigdata.edu.ddmath.util
 * @createTime: 2015/11/12 19:19
 */
public class AppUtils {

    /**
     * 获取本包信息
     *
     * @param context
     * @return
     */
    public static PackageInfo getPackageInfo(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            return info;
        } catch (Exception e) {
            AppLog.i("err", e);
            return null;
        }
    }

    /**
     * 获取手机序列化
     *
     * @param context
     * @return
     */
    public static String getDeviceId(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = telephonyManager.getDeviceId();
        if (Strings.isEmpty(deviceId)) {
            // 取AndroidId
            deviceId = getAndroidId(context);
        }
        if (Strings.isEmpty(deviceId)) {
            // 无效deviceId
            deviceId = "00000-00000";
        }
        return deviceId;
    }

    public static String getAndroidId(Context context) {
        return "id" + Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    /**
     * 是否无网络环境
     *
     * @return
     */
    public static boolean isNoNetwork() {
        Integer state = AppSession.getInstance().get(AppConst.SESSION_NETWORK_STSTE);
        return state != null && state == NetworkStateRecevie.NONETWORK;
    }

    /**
     * 是否Wifi环境
     *
     * @return
     */
    public static boolean isWifi() {
        Integer state = AppSession.getInstance().get(AppConst.SESSION_NETWORK_STSTE);
        return state != null && state == NetworkStateRecevie.WIFI;
    }

    /**
     * 是否是Debug模式
     *
     * @return
     */
    public static boolean isDebug() {
        return "debug".equals(BuildConfig.BUILD_TYPE);
    }
    public static boolean isBeta() {
        return "beta".equals(BuildConfig.BUILD_TYPE);
    }
    /**
     * 是否是release模式
     *
     * @return
     */
    public static boolean isRelease() {
        return "release".equals(BuildConfig.BUILD_TYPE);
    }

    /**
     * 是否前端运行
     *
     * @param context
     * @return
     */
    public static boolean isRunningForeground(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        String currentPackageName = cn.getPackageName();
        if (!TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(context.getPackageName())) {
            return true;
        }

        return false;
    }

    public static ActivityManager.RunningTaskInfo getTopTask(Context context) {
        ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = mActivityManager.getRunningTasks(1);
        if (tasks != null && !tasks.isEmpty()) {
            return tasks.get(0);
        }
        return null;
    }

    /**
     * 判断目标页面是否运行在前端
     *
     * @param topTask
     * @param packageName
     * @param activityName
     * @return
     */
    public static boolean isTopActivity(
            ActivityManager.RunningTaskInfo topTask,
            String packageName,
            String activityName) {
        if (topTask != null) {
            ComponentName topActivity = topTask.topActivity;

            if (topActivity.getPackageName().equals(packageName) &&
                    topActivity.getClassName().equals(activityName)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isWeixinAvilible(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * 判断qq是否可用
     *
     * @param context
     * @return
     */
    public static boolean isQQClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        return false;
    }
}
